package form;

import connection.StreamEvent;
import connection.ConnectionManager;
import connection.HostManager;
import connection.StreamEvent;
import connection.StreamListener;
import java.awt.event.ActionEvent;
import sun.audio.*; 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import logic.ChessBoard;
import logic.Empty;//added for test methods
import logic.King;//added for test methods
import logic.Pawn;//added for test methods
import logic.Queen;//added for test methods

import javax.swing.BorderFactory;
import java.awt.Component;
import java.awt.Color;
import java.awt.Point;
import logic.Player;

import java.awt.GridLayout;//added for test window
import javax.swing.JButton;//added for test window
import javax.swing.JComponent;
import javax.swing.JFrame;//added for test window
import javax.swing.JPanel;

import p2pExceptions.BufferNotReadyException;
import utilities.ReadFile;
import utilities.WriteFile;

/**
 *
 * @author hz
 */
public class MainFrame extends javax.swing.JFrame {

    /** Creates new form mainFrame */
    public MainFrame() {

        initComponents();
        board = new ChessBoard();
        whiteAligned = true;
        capture = false;
        promoted = false;
        enPassant = false;
        castled = false;
        clicked = null;
        sourceSquare = null;
        moveCount = 1;
        currentPlayer = new Player("");
        currentPlayer.setColor(Color.WHITE);//notation flakes out sometimes if I don't call this here
        currentPlayer.setMyTurn(true); //notation flakes out sometimes if I don't call this here
    }

    private void initNew() {
        board = new ChessBoard();
        capture = false;
        promoted = false;
        enPassant = false;
        castled = false;
        clicked = null;
        sourceSquare = null;
        moveCount = 1;
        moveList.removeAll();
        removeBorders();
        a1.add(whiteRook1);
        a1.repaint();
        a2.add(whitePawnA);
        a2.repaint();
        a7.add(blackPawnA);
        a7.repaint();
        a8.add(blackRook1);
        a8.repaint();
        b1.add(whiteKnight1);
        b1.repaint();
        b2.add(whitePawnB);
        b2.repaint();
        b7.add(blackPawnB);
        b7.repaint();
        b8.add(blackKnight1);
        b8.repaint();
        c1.add(whiteDarkBishop);
        c1.repaint();
        c2.add(whitePawnC);
        c2.repaint();
        c7.add(blackPawnC);
        c7.repaint();
        c8.add(blackLightBishop);
        c8.repaint();
        d1.add(whiteQueen);
        d1.repaint();
        d2.add(whitePawnD);
        d2.repaint();
        d7.add(blackPawnD);
        d7.repaint();
        d8.add(blackQueen);
        d8.repaint();
        e1.add(whiteKing);
        e1.repaint();
        e2.add(whitePawnE);
        e2.repaint();
        e7.add(blackPawnE);
        e7.repaint();
        e8.add(blackKing);
        e8.repaint();
        f1.add(whiteLightBishop);
        f1.repaint();
        f2.add(whitePawnF);
        f2.repaint();
        f7.add(blackPawnF);
        f7.repaint();
        f8.add(blackDarkBishop);
        f8.repaint();
        g1.add(whiteKnight2);
        g1.repaint();
        g2.add(whitePawnG);
        g2.repaint();
        g7.add(blackPawnG);
        g7.repaint();
        g8.add(blackKnight2);
        g8.repaint();
        h1.add(whiteRook2);
        h1.repaint();
        h2.add(whitePawnH);
        h2.repaint();
        h7.add(blackPawnH);
        h7.repaint();
        h8.add(blackRook2);
        h7.repaint();
        
        if (connMan==null && hostMan==null){
        	currentPlayer = new Player(""); 
    	    currentPlayer.setColor(Color.WHITE);
    	    currentPlayer.setMyTurn(true);
        }
    }//end initNew
    
    
    private void setPlayerColor(){
        if (newGame){ 
        	currentPlayer.randomColor();  //sets color AND turn
        }
        if (!newGame && connMan != null){
                currentPlayer.setColor(connMan.getConnColor());
                if(currentPlayer.getColor() == Color.WHITE){
                    currentPlayer.setMyTurn(true);
                }
                else
                    currentPlayer.setMyTurn(false);
        }
        	if (currentPlayer.getColor()==Color.BLACK){
        		System.out.println("I'm black");
        		flipMenuItemActionPerformed(null);
        		while (!colorConfirmed){
        			connMan.sendData("9:0");//tell the opponent that they are white
        			wait(500);
        		}
        	} else {
        		System.out.println("I'm white");
        		while (!colorConfirmed){
        			connMan.sendData("9:1");//tell the opponent that they are white
        			wait(500);
        		}
        	}
        	newGame=false;        
    }//end setting player color

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        moveList = new java.awt.List();
        boardPanel = new javax.swing.JPanel();
        a1 = new javax.swing.JPanel();
        whiteRook1 = new javax.swing.JLabel();
        a2 = new javax.swing.JPanel();
        whitePawnA = new javax.swing.JLabel();
        a3 = new javax.swing.JPanel();
        a4 = new javax.swing.JPanel();
        a5 = new javax.swing.JPanel();
        a6 = new javax.swing.JPanel();
        a7 = new javax.swing.JPanel();
        blackPawnA = new javax.swing.JLabel();
        a8 = new javax.swing.JPanel();
        blackRook1 = new javax.swing.JLabel();
        b1 = new javax.swing.JPanel();
        whiteKnight1 = new javax.swing.JLabel();
        b2 = new javax.swing.JPanel();
        whitePawnB = new javax.swing.JLabel();
        b3 = new javax.swing.JPanel();
        b4 = new javax.swing.JPanel();
        b5 = new javax.swing.JPanel();
        b6 = new javax.swing.JPanel();
        b7 = new javax.swing.JPanel();
        blackPawnB = new javax.swing.JLabel();
        b8 = new javax.swing.JPanel();
        blackKnight1 = new javax.swing.JLabel();
        c1 = new javax.swing.JPanel();
        whiteDarkBishop = new javax.swing.JLabel();
        c2 = new javax.swing.JPanel();
        whitePawnC = new javax.swing.JLabel();
        c3 = new javax.swing.JPanel();
        c4 = new javax.swing.JPanel();
        c5 = new javax.swing.JPanel();
        c6 = new javax.swing.JPanel();
        c7 = new javax.swing.JPanel();
        blackPawnC = new javax.swing.JLabel();
        c8 = new javax.swing.JPanel();
        blackLightBishop = new javax.swing.JLabel();
        d1 = new javax.swing.JPanel();
        whiteQueen = new javax.swing.JLabel();
        d2 = new javax.swing.JPanel();
        whitePawnD = new javax.swing.JLabel();
        d3 = new javax.swing.JPanel();
        d4 = new javax.swing.JPanel();
        d5 = new javax.swing.JPanel();
        d6 = new javax.swing.JPanel();
        d7 = new javax.swing.JPanel();
        blackPawnD = new javax.swing.JLabel();
        d8 = new javax.swing.JPanel();
        blackQueen = new javax.swing.JLabel();
        e1 = new javax.swing.JPanel();
        whiteKing = new javax.swing.JLabel();
        e2 = new javax.swing.JPanel();
        whitePawnE = new javax.swing.JLabel();
        e3 = new javax.swing.JPanel();
        e4 = new javax.swing.JPanel();
        e5 = new javax.swing.JPanel();
        e6 = new javax.swing.JPanel();
        e7 = new javax.swing.JPanel();
        blackPawnE = new javax.swing.JLabel();
        e8 = new javax.swing.JPanel();
        blackKing = new javax.swing.JLabel();
        f1 = new javax.swing.JPanel();
        whiteLightBishop = new javax.swing.JLabel();
        f2 = new javax.swing.JPanel();
        whitePawnF = new javax.swing.JLabel();
        f3 = new javax.swing.JPanel();
        f4 = new javax.swing.JPanel();
        f5 = new javax.swing.JPanel();
        f6 = new javax.swing.JPanel();
        f7 = new javax.swing.JPanel();
        blackPawnF = new javax.swing.JLabel();
        f8 = new javax.swing.JPanel();
        blackDarkBishop = new javax.swing.JLabel();
        g1 = new javax.swing.JPanel();
        whiteKnight2 = new javax.swing.JLabel();
        g2 = new javax.swing.JPanel();
        whitePawnG = new javax.swing.JLabel();
        g3 = new javax.swing.JPanel();
        g4 = new javax.swing.JPanel();
        g5 = new javax.swing.JPanel();
        g6 = new javax.swing.JPanel();
        g7 = new javax.swing.JPanel();
        blackPawnG = new javax.swing.JLabel();
        g8 = new javax.swing.JPanel();
        blackKnight2 = new javax.swing.JLabel();
        h1 = new javax.swing.JPanel();
        whiteRook2 = new javax.swing.JLabel();
        h2 = new javax.swing.JPanel();
        whitePawnH = new javax.swing.JLabel();
        h3 = new javax.swing.JPanel();
        h4 = new javax.swing.JPanel();
        h5 = new javax.swing.JPanel();
        h6 = new javax.swing.JPanel();
        h7 = new javax.swing.JPanel();
        blackPawnH = new javax.swing.JLabel();
        h8 = new javax.swing.JPanel();
        blackRook2 = new javax.swing.JLabel();
        boardLabel = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        connectMenuItem = new javax.swing.JMenuItem();
        hostMenuItem = new javax.swing.JMenuItem();
        disconnectMenuItem = new javax.swing.JMenuItem();
        fileMenuSeparator1 = new javax.swing.JPopupMenu.Separator();
        exitMenuItem = new javax.swing.JMenuItem();
        gameMenu = new javax.swing.JMenu();
        flipMenuItem = new javax.swing.JMenuItem();
        gameMenuSeparator1 = new javax.swing.JPopupMenu.Separator();
        newGameMenuItem = new javax.swing.JMenuItem();
        saveGameMenuItem = new javax.swing.JMenuItem();
        loadGameMenuItem = new javax.swing.JMenuItem();
        gameMenuSeparator2 = new javax.swing.JPopupMenu.Separator();
        takebackMenuItem = new javax.swing.JMenuItem();
        resignMenuItem = new javax.swing.JMenuItem();
        drawMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        instructionsMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(100, 100, 400, 400));
        setMinimumSize(new java.awt.Dimension(614, 522));
        setResizable(false);
        getContentPane().setLayout(null);
        getContentPane().add(moveList);
        moveList.setBounds(466, 0, 145, 474);

        boardPanel.setLayout(null);

        a1.setName("a1"); // NOI18N
        a1.setOpaque(false);
        a1.setPreferredSize(new java.awt.Dimension(44, 44));
        a1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a1MouseClicked(evt);
            }
        });
        a1.setLayout(null);

        whiteRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wr.png"))); // NOI18N
        a1.add(whiteRook1);
        whiteRook1.setBounds(5, 5, 36, 36);

        boardPanel.add(a1);
        a1.setBounds(37, 380, 48, 48);
        a1.getAccessibleContext().setAccessibleName("a1");

        a2.setName("a2"); // NOI18N
        a2.setOpaque(false);
        a2.setPreferredSize(new java.awt.Dimension(44, 44));
        a2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a2MouseClicked(evt);
            }
        });
        a2.setLayout(null);

        whitePawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        a2.add(whitePawnA);
        whitePawnA.setBounds(5, 5, 36, 36);

        boardPanel.add(a2);
        a2.setBounds(37, 331, 48, 48);
        a2.getAccessibleContext().setAccessibleName("a2");

        a3.setName("a3"); // NOI18N
        a3.setOpaque(false);
        a3.setPreferredSize(new java.awt.Dimension(44, 44));
        a3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a3MouseClicked(evt);
            }
        });
        a3.setLayout(null);
        boardPanel.add(a3);
        a3.setBounds(37, 282, 48, 48);
        a3.getAccessibleContext().setAccessibleName("a3");

        a4.setName("a4"); // NOI18N
        a4.setOpaque(false);
        a4.setPreferredSize(new java.awt.Dimension(44, 44));
        a4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a4MouseClicked(evt);
            }
        });
        a4.setLayout(null);
        boardPanel.add(a4);
        a4.setBounds(37, 234, 48, 47);
        a4.getAccessibleContext().setAccessibleName("a4");

        a5.setName("a5"); // NOI18N
        a5.setOpaque(false);
        a5.setPreferredSize(new java.awt.Dimension(44, 44));
        a5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a5MouseClicked(evt);
            }
        });
        a5.setLayout(null);
        boardPanel.add(a5);
        a5.setBounds(37, 186, 48, 47);

        a6.setName("a6"); // NOI18N
        a6.setOpaque(false);
        a6.setPreferredSize(new java.awt.Dimension(44, 44));
        a6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a6MouseClicked(evt);
            }
        });
        a6.setLayout(null);
        boardPanel.add(a6);
        a6.setBounds(37, 138, 48, 47);

        a7.setName("a7"); // NOI18N
        a7.setOpaque(false);
        a7.setPreferredSize(new java.awt.Dimension(44, 44));
        a7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a7MouseClicked(evt);
            }
        });

        blackPawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N

        javax.swing.GroupLayout a7Layout = new javax.swing.GroupLayout(a7);
        a7.setLayout(a7Layout);
        a7Layout.setHorizontalGroup(
            a7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 48, Short.MAX_VALUE)
            .addGroup(a7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(a7Layout.createSequentialGroup()
                    .addGap(0, 6, Short.MAX_VALUE)
                    .addComponent(blackPawnA)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );
        a7Layout.setVerticalGroup(
            a7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 47, Short.MAX_VALUE)
            .addGroup(a7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(a7Layout.createSequentialGroup()
                    .addGap(0, 5, Short.MAX_VALUE)
                    .addComponent(blackPawnA)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );

        boardPanel.add(a7);
        a7.setBounds(37, 90, 48, 47);

        a8.setName("a8"); // NOI18N
        a8.setOpaque(false);
        a8.setPreferredSize(new java.awt.Dimension(44, 44));
        a8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                a8MouseClicked(evt);
            }
        });
        a8.setLayout(null);

        blackRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/br.png"))); // NOI18N
        a8.add(blackRook1);
        blackRook1.setBounds(5, 5, 36, 36);

        boardPanel.add(a8);
        a8.setBounds(37, 42, 48, 47);

        b1.setName("b1"); // NOI18N
        b1.setOpaque(false);
        b1.setPreferredSize(new java.awt.Dimension(44, 44));
        b1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b1MouseClicked(evt);
            }
        });
        b1.setLayout(null);

        whiteKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wn.png"))); // NOI18N
        b1.add(whiteKnight1);
        whiteKnight1.setBounds(5, 5, 36, 36);

        boardPanel.add(b1);
        b1.setBounds(86, 380, 47, 47);

        b2.setName("b2"); // NOI18N
        b2.setOpaque(false);
        b2.setPreferredSize(new java.awt.Dimension(44, 44));
        b2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b2MouseClicked(evt);
            }
        });
        b2.setLayout(null);

        whitePawnB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        b2.add(whitePawnB);
        whitePawnB.setBounds(5, 5, 36, 36);

        boardPanel.add(b2);
        b2.setBounds(86, 331, 47, 48);

        b3.setName("b3"); // NOI18N
        b3.setOpaque(false);
        b3.setPreferredSize(new java.awt.Dimension(44, 44));
        b3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b3MouseClicked(evt);
            }
        });
        b3.setLayout(null);
        boardPanel.add(b3);
        b3.setBounds(86, 283, 47, 47);

        b4.setName("b4"); // NOI18N
        b4.setOpaque(false);
        b4.setPreferredSize(new java.awt.Dimension(44, 44));
        b4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b4MouseClicked(evt);
            }
        });
        b4.setLayout(null);
        boardPanel.add(b4);
        b4.setBounds(86, 234, 47, 48);

        b5.setName("b5"); // NOI18N
        b5.setOpaque(false);
        b5.setPreferredSize(new java.awt.Dimension(44, 44));
        b5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b5MouseClicked(evt);
            }
        });
        b5.setLayout(null);
        boardPanel.add(b5);
        b5.setBounds(86, 186, 47, 47);

        b6.setName("b6"); // NOI18N
        b6.setOpaque(false);
        b6.setPreferredSize(new java.awt.Dimension(44, 44));
        b6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b6MouseClicked(evt);
            }
        });
        b6.setLayout(null);
        boardPanel.add(b6);
        b6.setBounds(86, 138, 47, 47);

        b7.setName("b7"); // NOI18N
        b7.setOpaque(false);
        b7.setPreferredSize(new java.awt.Dimension(44, 44));
        b7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b7MouseClicked(evt);
            }
        });

        blackPawnB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N

        javax.swing.GroupLayout b7Layout = new javax.swing.GroupLayout(b7);
        b7.setLayout(b7Layout);
        b7Layout.setHorizontalGroup(
            b7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 47, Short.MAX_VALUE)
            .addGroup(b7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(b7Layout.createSequentialGroup()
                    .addGap(0, 5, Short.MAX_VALUE)
                    .addComponent(blackPawnB)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );
        b7Layout.setVerticalGroup(
            b7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 47, Short.MAX_VALUE)
            .addGroup(b7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(b7Layout.createSequentialGroup()
                    .addGap(0, 5, Short.MAX_VALUE)
                    .addComponent(blackPawnB)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );

        boardPanel.add(b7);
        b7.setBounds(86, 90, 47, 47);

        b8.setName("b8"); // NOI18N
        b8.setOpaque(false);
        b8.setPreferredSize(new java.awt.Dimension(44, 44));
        b8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                b8MouseClicked(evt);
            }
        });
        b8.setLayout(null);

        blackKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bn.png"))); // NOI18N
        b8.add(blackKnight1);
        blackKnight1.setBounds(5, 5, 36, 36);

        boardPanel.add(b8);
        b8.setBounds(86, 42, 47, 47);

        c1.setName("c1"); // NOI18N
        c1.setOpaque(false);
        c1.setPreferredSize(new java.awt.Dimension(44, 44));
        c1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c1MouseClicked(evt);
            }
        });
        c1.setLayout(null);

        whiteDarkBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wb.png"))); // NOI18N
        c1.add(whiteDarkBishop);
        whiteDarkBishop.setBounds(5, 5, 36, 36);

        boardPanel.add(c1);
        c1.setBounds(134, 380, 48, 47);

        c2.setName("c2"); // NOI18N
        c2.setOpaque(false);
        c2.setPreferredSize(new java.awt.Dimension(44, 44));
        c2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c2MouseClicked(evt);
            }
        });
        c2.setLayout(null);

        whitePawnC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        c2.add(whitePawnC);
        whitePawnC.setBounds(5, 5, 36, 36);

        boardPanel.add(c2);
        c2.setBounds(134, 331, 48, 47);

        c3.setName("c3"); // NOI18N
        c3.setOpaque(false);
        c3.setPreferredSize(new java.awt.Dimension(44, 44));
        c3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c3MouseClicked(evt);
            }
        });
        c3.setLayout(null);
        boardPanel.add(c3);
        c3.setBounds(134, 283, 48, 47);

        c4.setName("c4"); // NOI18N
        c4.setOpaque(false);
        c4.setPreferredSize(new java.awt.Dimension(44, 44));
        c4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c4MouseClicked(evt);
            }
        });
        c4.setLayout(null);
        boardPanel.add(c4);
        c4.setBounds(134, 234, 48, 47);

        c5.setName("c5"); // NOI18N
        c5.setOpaque(false);
        c5.setPreferredSize(new java.awt.Dimension(44, 44));
        c5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c5MouseClicked(evt);
            }
        });
        c5.setLayout(null);
        boardPanel.add(c5);
        c5.setBounds(134, 186, 48, 47);

        c6.setName("c6"); // NOI18N
        c6.setOpaque(false);
        c6.setPreferredSize(new java.awt.Dimension(44, 44));
        c6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c6MouseClicked(evt);
            }
        });
        c6.setLayout(null);
        boardPanel.add(c6);
        c6.setBounds(134, 138, 48, 47);

        c7.setName("c7"); // NOI18N
        c7.setOpaque(false);
        c7.setPreferredSize(new java.awt.Dimension(44, 44));
        c7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c7MouseClicked(evt);
            }
        });

        blackPawnC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N

        javax.swing.GroupLayout c7Layout = new javax.swing.GroupLayout(c7);
        c7.setLayout(c7Layout);
        c7Layout.setHorizontalGroup(
            c7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 48, Short.MAX_VALUE)
            .addGroup(c7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(c7Layout.createSequentialGroup()
                    .addGap(0, 6, Short.MAX_VALUE)
                    .addComponent(blackPawnC)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );
        c7Layout.setVerticalGroup(
            c7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 47, Short.MAX_VALUE)
            .addGroup(c7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(c7Layout.createSequentialGroup()
                    .addGap(0, 5, Short.MAX_VALUE)
                    .addComponent(blackPawnC)
                    .addGap(0, 6, Short.MAX_VALUE)))
        );

        boardPanel.add(c7);
        c7.setBounds(134, 90, 48, 47);

        c8.setName("c8"); // NOI18N
        c8.setOpaque(false);
        c8.setPreferredSize(new java.awt.Dimension(44, 44));
        c8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                c8MouseClicked(evt);
            }
        });
        c8.setLayout(null);

        blackLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bb.png"))); // NOI18N
        c8.add(blackLightBishop);
        blackLightBishop.setBounds(5, 3, 40, 40);

        boardPanel.add(c8);
        c8.setBounds(134, 42, 48, 47);

        d1.setName("d1"); // NOI18N
        d1.setOpaque(false);
        d1.setPreferredSize(new java.awt.Dimension(44, 44));
        d1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d1MouseClicked(evt);
            }
        });
        d1.setLayout(null);

        whiteQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wq.png"))); // NOI18N
        d1.add(whiteQueen);
        whiteQueen.setBounds(5, 5, 36, 36);

        boardPanel.add(d1);
        d1.setBounds(183, 380, 47, 47);

        d2.setName("d2"); // NOI18N
        d2.setOpaque(false);
        d2.setPreferredSize(new java.awt.Dimension(44, 44));
        d2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d2MouseClicked(evt);
            }
        });
        d2.setLayout(null);

        whitePawnD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        d2.add(whitePawnD);
        whitePawnD.setBounds(5, 5, 36, 36);

        boardPanel.add(d2);
        d2.setBounds(183, 331, 47, 48);

        d3.setName("d3"); // NOI18N
        d3.setOpaque(false);
        d3.setPreferredSize(new java.awt.Dimension(44, 44));
        d3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d3MouseClicked(evt);
            }
        });
        d3.setLayout(null);
        boardPanel.add(d3);
        d3.setBounds(183, 283, 47, 47);

        d4.setName("d4"); // NOI18N
        d4.setOpaque(false);
        d4.setPreferredSize(new java.awt.Dimension(44, 44));
        d4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d4MouseClicked(evt);
            }
        });
        d4.setLayout(null);
        boardPanel.add(d4);
        d4.setBounds(183, 234, 47, 48);

        d5.setName("d5"); // NOI18N
        d5.setOpaque(false);
        d5.setPreferredSize(new java.awt.Dimension(44, 44));
        d5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d5MouseClicked(evt);
            }
        });
        d5.setLayout(null);
        boardPanel.add(d5);
        d5.setBounds(183, 186, 47, 47);

        d6.setName("d6"); // NOI18N
        d6.setOpaque(false);
        d6.setPreferredSize(new java.awt.Dimension(44, 44));
        d6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d6MouseClicked(evt);
            }
        });
        d6.setLayout(null);
        boardPanel.add(d6);
        d6.setBounds(183, 138, 47, 47);

        d7.setName("d7"); // NOI18N
        d7.setOpaque(false);
        d7.setPreferredSize(new java.awt.Dimension(44, 44));
        d7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d7MouseClicked(evt);
            }
        });
        d7.setLayout(null);

        blackPawnD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N
        d7.add(blackPawnD);
        blackPawnD.setBounds(5, 5, 36, 36);

        boardPanel.add(d7);
        d7.setBounds(183, 90, 47, 47);

        d8.setName("d8"); // NOI18N
        d8.setOpaque(false);
        d8.setPreferredSize(new java.awt.Dimension(44, 44));
        d8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                d8MouseClicked(evt);
            }
        });
        d8.setLayout(null);

        blackQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bq.png"))); // NOI18N
        d8.add(blackQueen);
        blackQueen.setBounds(5, 4, 40, 40);

        boardPanel.add(d8);
        d8.setBounds(183, 42, 47, 47);

        e1.setName("e1"); // NOI18N
        e1.setOpaque(false);
        e1.setPreferredSize(new java.awt.Dimension(44, 44));
        e1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e1MouseClicked(evt);
            }
        });
        e1.setLayout(null);

        whiteKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wk.png"))); // NOI18N
        e1.add(whiteKing);
        whiteKing.setBounds(5, 5, 36, 36);

        boardPanel.add(e1);
        e1.setBounds(231, 380, 47, 47);

        e2.setName("e2"); // NOI18N
        e2.setOpaque(false);
        e2.setPreferredSize(new java.awt.Dimension(44, 44));
        e2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e2MouseClicked(evt);
            }
        });
        e2.setLayout(null);

        whitePawnE.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        e2.add(whitePawnE);
        whitePawnE.setBounds(5, 5, 36, 36);

        boardPanel.add(e2);
        e2.setBounds(231, 331, 47, 47);

        e3.setName("e3"); // NOI18N
        e3.setOpaque(false);
        e3.setPreferredSize(new java.awt.Dimension(44, 44));
        e3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e3MouseClicked(evt);
            }
        });
        e3.setLayout(null);
        boardPanel.add(e3);
        e3.setBounds(231, 283, 47, 47);

        e4.setName("e4"); // NOI18N
        e4.setOpaque(false);
        e4.setPreferredSize(new java.awt.Dimension(44, 44));
        e4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e4MouseClicked(evt);
            }
        });
        e4.setLayout(null);
        boardPanel.add(e4);
        e4.setBounds(231, 234, 47, 47);

        e5.setName("e5"); // NOI18N
        e5.setOpaque(false);
        e5.setPreferredSize(new java.awt.Dimension(44, 44));
        e5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e5MouseClicked(evt);
            }
        });
        e5.setLayout(null);
        boardPanel.add(e5);
        e5.setBounds(231, 186, 47, 47);

        e6.setName("e6"); // NOI18N
        e6.setOpaque(false);
        e6.setPreferredSize(new java.awt.Dimension(44, 44));
        e6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e6MouseClicked(evt);
            }
        });
        e6.setLayout(null);
        boardPanel.add(e6);
        e6.setBounds(231, 138, 47, 47);

        e7.setName("e7"); // NOI18N
        e7.setOpaque(false);
        e7.setPreferredSize(new java.awt.Dimension(44, 44));
        e7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e7MouseClicked(evt);
            }
        });
        e7.setLayout(null);

        blackPawnE.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N
        e7.add(blackPawnE);
        blackPawnE.setBounds(5, 5, 36, 36);

        boardPanel.add(e7);
        e7.setBounds(231, 90, 47, 47);

        e8.setName("e8"); // NOI18N
        e8.setOpaque(false);
        e8.setPreferredSize(new java.awt.Dimension(44, 44));
        e8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                e8MouseClicked(evt);
            }
        });
        e8.setLayout(null);

        blackKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bk.png"))); // NOI18N
        e8.add(blackKing);
        blackKing.setBounds(5, 5, 36, 36);

        boardPanel.add(e8);
        e8.setBounds(231, 42, 47, 47);

        f1.setName("f1"); // NOI18N
        f1.setOpaque(false);
        f1.setPreferredSize(new java.awt.Dimension(44, 44));
        f1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f1MouseClicked(evt);
            }
        });
        f1.setLayout(null);

        whiteLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wb.png"))); // NOI18N
        f1.add(whiteLightBishop);
        whiteLightBishop.setBounds(5, 5, 36, 36);

        boardPanel.add(f1);
        f1.setBounds(279, 380, 48, 47);

        f2.setName("f2"); // NOI18N
        f2.setOpaque(false);
        f2.setPreferredSize(new java.awt.Dimension(44, 44));
        f2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f2MouseClicked(evt);
            }
        });
        f2.setLayout(null);

        whitePawnF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        f2.add(whitePawnF);
        whitePawnF.setBounds(5, 5, 36, 36);

        boardPanel.add(f2);
        f2.setBounds(279, 331, 48, 48);

        f3.setName("f3"); // NOI18N
        f3.setOpaque(false);
        f3.setPreferredSize(new java.awt.Dimension(44, 44));
        f3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f3MouseClicked(evt);
            }
        });
        f3.setLayout(null);
        boardPanel.add(f3);
        f3.setBounds(279, 283, 48, 47);

        f4.setName("f4"); // NOI18N
        f4.setOpaque(false);
        f4.setPreferredSize(new java.awt.Dimension(44, 44));
        f4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f4MouseClicked(evt);
            }
        });
        f4.setLayout(null);
        boardPanel.add(f4);
        f4.setBounds(279, 234, 48, 48);

        f5.setName("f5"); // NOI18N
        f5.setOpaque(false);
        f5.setPreferredSize(new java.awt.Dimension(44, 44));
        f5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f5MouseClicked(evt);
            }
        });
        f5.setLayout(null);
        boardPanel.add(f5);
        f5.setBounds(279, 186, 48, 47);

        f6.setName("f6"); // NOI18N
        f6.setOpaque(false);
        f6.setPreferredSize(new java.awt.Dimension(44, 44));
        f6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f6MouseClicked(evt);
            }
        });
        f6.setLayout(null);
        boardPanel.add(f6);
        f6.setBounds(279, 138, 48, 47);

        f7.setMinimumSize(new java.awt.Dimension(48, 48));
        f7.setName("f7"); // NOI18N
        f7.setOpaque(false);
        f7.setPreferredSize(new java.awt.Dimension(44, 44));
        f7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f7MouseClicked(evt);
            }
        });
        f7.setLayout(null);

        blackPawnF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N
        f7.add(blackPawnF);
        blackPawnF.setBounds(6, 5, 36, 36);

        boardPanel.add(f7);
        f7.setBounds(279, 90, 48, 47);

        f8.setName("f8"); // NOI18N
        f8.setOpaque(false);
        f8.setPreferredSize(new java.awt.Dimension(44, 44));
        f8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                f8MouseClicked(evt);
            }
        });
        f8.setLayout(null);

        blackDarkBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bb.png"))); // NOI18N
        f8.add(blackDarkBishop);
        blackDarkBishop.setBounds(6, 4, 40, 40);

        boardPanel.add(f8);
        f8.setBounds(279, 42, 48, 47);

        g1.setName("g1"); // NOI18N
        g1.setOpaque(false);
        g1.setPreferredSize(new java.awt.Dimension(44, 44));
        g1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g1MouseClicked(evt);
            }
        });
        g1.setLayout(null);

        whiteKnight2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wn.png"))); // NOI18N
        g1.add(whiteKnight2);
        whiteKnight2.setBounds(5, 5, 36, 36);

        boardPanel.add(g1);
        g1.setBounds(328, 380, 47, 47);

        g2.setName("g2"); // NOI18N
        g2.setOpaque(false);
        g2.setPreferredSize(new java.awt.Dimension(44, 44));
        g2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g2MouseClicked(evt);
            }
        });
        g2.setLayout(null);

        whitePawnG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        g2.add(whitePawnG);
        whitePawnG.setBounds(5, 5, 36, 36);

        boardPanel.add(g2);
        g2.setBounds(328, 331, 47, 47);

        g3.setName("g3"); // NOI18N
        g3.setOpaque(false);
        g3.setPreferredSize(new java.awt.Dimension(44, 44));
        g3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g3MouseClicked(evt);
            }
        });
        g3.setLayout(null);
        boardPanel.add(g3);
        g3.setBounds(328, 283, 47, 47);

        g4.setName("g4"); // NOI18N
        g4.setOpaque(false);
        g4.setPreferredSize(new java.awt.Dimension(44, 44));
        g4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g4MouseClicked(evt);
            }
        });
        g4.setLayout(null);
        boardPanel.add(g4);
        g4.setBounds(328, 234, 47, 47);

        g5.setName("g5"); // NOI18N
        g5.setOpaque(false);
        g5.setPreferredSize(new java.awt.Dimension(44, 44));
        g5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g5MouseClicked(evt);
            }
        });
        g5.setLayout(null);
        boardPanel.add(g5);
        g5.setBounds(328, 186, 47, 47);

        g6.setName("g6"); // NOI18N
        g6.setOpaque(false);
        g6.setPreferredSize(new java.awt.Dimension(44, 44));
        g6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g6MouseClicked(evt);
            }
        });
        g6.setLayout(null);
        boardPanel.add(g6);
        g6.setBounds(328, 138, 47, 47);

        g7.setName("g7"); // NOI18N
        g7.setOpaque(false);
        g7.setPreferredSize(new java.awt.Dimension(44, 44));
        g7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g7MouseClicked(evt);
            }
        });
        g7.setLayout(null);

        blackPawnG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N
        g7.add(blackPawnG);
        blackPawnG.setBounds(5, 5, 36, 36);

        boardPanel.add(g7);
        g7.setBounds(328, 90, 47, 47);

        g8.setName("g8"); // NOI18N
        g8.setOpaque(false);
        g8.setPreferredSize(new java.awt.Dimension(44, 44));
        g8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                g8MouseClicked(evt);
            }
        });
        g8.setLayout(null);

        blackKnight2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bn.png"))); // NOI18N
        g8.add(blackKnight2);
        blackKnight2.setBounds(5, 5, 36, 36);

        boardPanel.add(g8);
        g8.setBounds(328, 42, 47, 47);

        h1.setName("h1"); // NOI18N
        h1.setOpaque(false);
        h1.setPreferredSize(new java.awt.Dimension(44, 44));
        h1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h1MouseClicked(evt);
            }
        });
        h1.setLayout(null);

        whiteRook2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wr.png"))); // NOI18N
        h1.add(whiteRook2);
        whiteRook2.setBounds(5, 5, 36, 36);

        boardPanel.add(h1);
        h1.setBounds(376, 381, 48, 47);

        h2.setName("h2"); // NOI18N
        h2.setOpaque(false);
        h2.setPreferredSize(new java.awt.Dimension(44, 44));
        h2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h2MouseClicked(evt);
            }
        });
        h2.setLayout(null);

        whitePawnH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png"))); // NOI18N
        h2.add(whitePawnH);
        whitePawnH.setBounds(5, 5, 36, 36);

        boardPanel.add(h2);
        h2.setBounds(376, 331, 48, 48);

        h3.setName("h3"); // NOI18N
        h3.setOpaque(false);
        h3.setPreferredSize(new java.awt.Dimension(44, 44));
        h3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h3MouseClicked(evt);
            }
        });
        h3.setLayout(null);
        boardPanel.add(h3);
        h3.setBounds(376, 283, 48, 47);

        h4.setName("h4"); // NOI18N
        h4.setOpaque(false);
        h4.setPreferredSize(new java.awt.Dimension(44, 44));
        h4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h4MouseClicked(evt);
            }
        });
        h4.setLayout(null);
        boardPanel.add(h4);
        h4.setBounds(376, 234, 48, 48);

        h5.setName("h5"); // NOI18N
        h5.setOpaque(false);
        h5.setPreferredSize(new java.awt.Dimension(44, 44));
        h5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h5MouseClicked(evt);
            }
        });
        h5.setLayout(null);
        boardPanel.add(h5);
        h5.setBounds(376, 186, 48, 47);

        h6.setName("h6"); // NOI18N
        h6.setOpaque(false);
        h6.setPreferredSize(new java.awt.Dimension(44, 44));
        h6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h6MouseClicked(evt);
            }
        });
        h6.setLayout(null);
        boardPanel.add(h6);
        h6.setBounds(376, 138, 48, 47);

        h7.setName("h7"); // NOI18N
        h7.setOpaque(false);
        h7.setPreferredSize(new java.awt.Dimension(44, 44));
        h7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h7MouseClicked(evt);
            }
        });
        h7.setLayout(null);

        blackPawnH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png"))); // NOI18N
        h7.add(blackPawnH);
        blackPawnH.setBounds(6, 5, 36, 36);

        boardPanel.add(h7);
        h7.setBounds(376, 90, 48, 47);

        h8.setName("h8"); // NOI18N
        h8.setOpaque(false);
        h8.setPreferredSize(new java.awt.Dimension(44, 44));
        h8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                h8MouseClicked(evt);
            }
        });
        h8.setLayout(null);

        blackRook2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/br.png"))); // NOI18N
        h8.add(blackRook2);
        blackRook2.setBounds(5, 5, 36, 36);

        boardPanel.add(h8);
        h8.setBounds(376, 42, 48, 47);

        boardLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/board.png"))); // NOI18N
        boardLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
        boardPanel.add(boardLabel);
        boardLabel.setBounds(0, 0, 464, 474);

        getContentPane().add(boardPanel);
        boardPanel.setBounds(0, 0, 470, 480);

        fileMenu.setMnemonic('f');
        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        connectMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        connectMenuItem.setMnemonic('c');
        connectMenuItem.setText("Connect");
        connectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(connectMenuItem);

        hostMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        hostMenuItem.setMnemonic('h');
        hostMenuItem.setText("Host");
        hostMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hostMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(hostMenuItem);

        disconnectMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        disconnectMenuItem.setMnemonic('d');
        disconnectMenuItem.setText("Disconnect");
        disconnectMenuItem.setEnabled(false);
        disconnectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disconnectMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(disconnectMenuItem);
        fileMenu.add(fileMenuSeparator1);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        gameMenu.setMnemonic('g');
        gameMenu.setText("Game");

        flipMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        flipMenuItem.setMnemonic('f');
        flipMenuItem.setText("Flip Board");
        flipMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                flipMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(flipMenuItem);
        gameMenu.add(gameMenuSeparator1);

        newGameMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newGameMenuItem.setText("New Game");
        newGameMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newGameMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(newGameMenuItem);

        saveGameMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveGameMenuItem.setMnemonic('s');
        saveGameMenuItem.setText("Save Game");
        saveGameMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(saveGameMenuItem);

        loadGameMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        loadGameMenuItem.setMnemonic('l');
        loadGameMenuItem.setText("Load Game");
        loadGameMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(loadGameMenuItem);
        gameMenu.add(gameMenuSeparator2);

        takebackMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        takebackMenuItem.setText("Request Takeback");
        takebackMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                takebackMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(takebackMenuItem);

        resignMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        resignMenuItem.setMnemonic('r');
        resignMenuItem.setText("Resign");
        resignMenuItem.setEnabled(false);
        resignMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resignActionPerformed(evt);
            }
        });
        gameMenu.add(resignMenuItem);

        drawMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        drawMenuItem.setMnemonic('d');
        drawMenuItem.setText("Offer Draw");
        drawMenuItem.setEnabled(false);
        drawMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                drawMenuItemActionPerformed(evt);
            }
        });
        gameMenu.add(drawMenuItem);

        menuBar.add(gameMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("Help");

        instructionsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        instructionsMenuItem.setText("Instructions");
        instructionsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                instructionsMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(instructionsMenuItem);

        aboutMenuItem.setText("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
    }//GEN-LAST:event_fileMenuActionPerformed
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
        if (connMan != null) {
	        connMan.sendData("8:");
	    } else if (hostMan != null) {
	        hostMan.sendData("8:");
	    }
        System.exit(0);
    }

    private void connectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectMenuItemActionPerformed
        connMan = new ConnectionManager();
        hostMan = null;
        conn = new ConnectDialog(this, true);
        conn.setLocationRelativeTo(this);
        conn.setVisible(true);
        newGame = true;

        if (connMan.isConnected() && !connMan.isLoad()) {
            hostMenuItem.setEnabled(false);
            connectMenuItem.setEnabled(false);
            newGameMenuItem.setEnabled(false);
            disconnectMenuItem.setEnabled(true);
            drawMenuItem.setEnabled(true);
            resignMenuItem.setEnabled(true);
            gameOver = false;
            
            connMan.start(); //start listening
            StreamListener stream = new StreamFetcher(); //sky needs to be teh call to process the data
            connMan.addStreamListener(stream);
            wipeBoard();//make a new board
            initNew();
            setPlayerColor();
        }
        else if(connMan.isConnected() && connMan.isLoad()){ //This is when coneected and conditions are matched to loadFile
            String brdHash = connMan.getHash();
            hostMenuItem.setEnabled(false);
            connectMenuItem.setEnabled(false);
            newGameMenuItem.setEnabled(false);
            disconnectMenuItem.setEnabled(true);
            drawMenuItem.setEnabled(true);
            resignMenuItem.setEnabled(true);
            gameOver = false;

            connMan.start(); //start listening
            StreamListener stream = new StreamFetcher(); //sky needs to be teh call to process the data
            connMan.addStreamListener(stream);
            wipeBoard();//make a new board
            initNew();
            newGame = false;            
            System.out.println(brdHash+" HASH");
            board.convertHash(brdHash);
            updatePieces();
            connMan.sendData("0:"+brdHash+":0:01:");
            setPlayerColor();//This is needs to be changes since saved game would have color difined
            
        }
        else {
            connMan = null;
        }
    }//GEN-LAST:event_connectMenuItemActionPerformed
    private void disconnectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disconnectMenuItemActionPerformed
        try {
            if (connMan != null) {
                connMan.sendData("8:");
                connMan.disconnect();
                if (!connMan.isConnected()) {
                    hostMenuItem.setEnabled(true);
                    connectMenuItem.setEnabled(true);
                    newGameMenuItem.setEnabled(true);
                    disconnectMenuItem.setEnabled(false);
                    drawMenuItem.setEnabled(false);
                    resignMenuItem.setEnabled(false);
                    connMan = null;
                }
            } else if (hostMan != null) {
                    hostMan.sendData("8:");
                    hostMan.stopHost();
                    if (!hostMan.isConnected()) {
                        hostMenuItem.setEnabled(true);
                        connectMenuItem.setEnabled(true);
                        newGameMenuItem.setEnabled(true);
                        disconnectMenuItem.setEnabled(false);
                        drawMenuItem.setEnabled(false);
                        resignMenuItem.setEnabled(false);
                        hostMan = null;
                    }
              }
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_disconnectMenuItemActionPerformed
    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        abox = new AboutBox(this, true);
        abox.setLocationRelativeTo(this);
        abox.setVisible(true);
    }//GEN-LAST:event_aboutMenuItemActionPerformed

    private void flipMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_flipMenuItemActionPerformed
        if (whiteAligned == true) {
            whiteAligned = false;
            boardLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/board-flip.png")));
        } else {
            whiteAligned = true;
            boardLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/board.png")));
        }
        // Flip the A column
        Point location = a1.getLocation();
        a1.setLocation(a8.getLocation());
        a8.setLocation(location);
        location = a2.getLocation();
        a2.setLocation(a7.getLocation());
        a7.setLocation(location);
        location = a3.getLocation();
        a3.setLocation(a6.getLocation());
        a6.setLocation(location);
        location = a4.getLocation();
        a4.setLocation(a5.getLocation());
        a5.setLocation(location);

        // Flip the B column
        location = b1.getLocation();
        b1.setLocation(b8.getLocation());
        b8.setLocation(location);
        location = b2.getLocation();
        b2.setLocation(b7.getLocation());
        b7.setLocation(location);
        location = b3.getLocation();
        b3.setLocation(b6.getLocation());
        b6.setLocation(location);
        location = b4.getLocation();
        b4.setLocation(b5.getLocation());
        b5.setLocation(location);

        // Flip the C column
        location = c1.getLocation();
        c1.setLocation(c8.getLocation());
        c8.setLocation(location);
        location = c2.getLocation();
        c2.setLocation(c7.getLocation());
        c7.setLocation(location);
        location = c3.getLocation();
        c3.setLocation(c6.getLocation());
        c6.setLocation(location);
        location = c4.getLocation();
        c4.setLocation(c5.getLocation());
        c5.setLocation(location);

        // Flip the D column
        location = d1.getLocation();
        d1.setLocation(d8.getLocation());
        d8.setLocation(location);
        location = d2.getLocation();
        d2.setLocation(d7.getLocation());
        d7.setLocation(location);
        location = d3.getLocation();
        d3.setLocation(d6.getLocation());
        d6.setLocation(location);
        location = d4.getLocation();
        d4.setLocation(d5.getLocation());
        d5.setLocation(location);

        // Flip the E column
        location = e1.getLocation();
        e1.setLocation(e8.getLocation());
        e8.setLocation(location);
        location = e2.getLocation();
        e2.setLocation(e7.getLocation());
        e7.setLocation(location);
        location = e3.getLocation();
        e3.setLocation(e6.getLocation());
        e6.setLocation(location);
        location = e4.getLocation();
        e4.setLocation(e5.getLocation());
        e5.setLocation(location);

        // Flip the F column
        location = f1.getLocation();
        f1.setLocation(f8.getLocation());
        f8.setLocation(location);
        location = f2.getLocation();
        f2.setLocation(f7.getLocation());
        f7.setLocation(location);
        location = f3.getLocation();
        f3.setLocation(f6.getLocation());
        f6.setLocation(location);
        location = f4.getLocation();
        f4.setLocation(f5.getLocation());
        f5.setLocation(location);

        // Flip the G column
        location = g1.getLocation();
        g1.setLocation(g8.getLocation());
        g8.setLocation(location);
        location = g2.getLocation();
        g2.setLocation(g7.getLocation());
        g7.setLocation(location);
        location = g3.getLocation();
        g3.setLocation(g6.getLocation());
        g6.setLocation(location);
        location = g4.getLocation();
        g4.setLocation(g5.getLocation());
        g5.setLocation(location);

        // Flip the H column
        location = h1.getLocation();
        h1.setLocation(h8.getLocation());
        h8.setLocation(location);
        location = h2.getLocation();
        h2.setLocation(h7.getLocation());
        h7.setLocation(location);
        location = h3.getLocation();
        h3.setLocation(h6.getLocation());
        h6.setLocation(location);
        location = h4.getLocation();
        h4.setLocation(h5.getLocation());
        h5.setLocation(location);

        // Flip Rank 1
        location = a1.getLocation();
        a1.setLocation(h1.getLocation());
        h1.setLocation(location);
        location = b1.getLocation();
        b1.setLocation(g1.getLocation());
        g1.setLocation(location);
        location = c1.getLocation();
        c1.setLocation(f1.getLocation());
        f1.setLocation(location);
        location = d1.getLocation();
        d1.setLocation(e1.getLocation());
        e1.setLocation(location);

        // Flip Rank 2
        location = a2.getLocation();
        a2.setLocation(h2.getLocation());
        h2.setLocation(location);
        location = b2.getLocation();
        b2.setLocation(g2.getLocation());
        g2.setLocation(location);
        location = c2.getLocation();
        c2.setLocation(f2.getLocation());
        f2.setLocation(location);
        location = d2.getLocation();
        d2.setLocation(e2.getLocation());
        e2.setLocation(location);

        // Flip Rank 3
        location = a3.getLocation();
        a3.setLocation(h3.getLocation());
        h3.setLocation(location);
        location = b3.getLocation();
        b3.setLocation(g3.getLocation());
        g3.setLocation(location);
        location = c3.getLocation();
        c3.setLocation(f3.getLocation());
        f3.setLocation(location);
        location = d3.getLocation();
        d3.setLocation(e3.getLocation());
        e3.setLocation(location);

        // Flip Rank 4
        location = a4.getLocation();
        a4.setLocation(h4.getLocation());
        h4.setLocation(location);
        location = b4.getLocation();
        b4.setLocation(g4.getLocation());
        g4.setLocation(location);
        location = c4.getLocation();
        c4.setLocation(f4.getLocation());
        f4.setLocation(location);
        location = d4.getLocation();
        d4.setLocation(e4.getLocation());
        e4.setLocation(location);

        // Flip Rank 5
        location = a5.getLocation();
        a5.setLocation(h5.getLocation());
        h5.setLocation(location);
        location = b5.getLocation();
        b5.setLocation(g5.getLocation());
        g5.setLocation(location);
        location = c5.getLocation();
        c5.setLocation(f5.getLocation());
        f5.setLocation(location);
        location = d5.getLocation();
        d5.setLocation(e5.getLocation());
        e5.setLocation(location);

        // Flip Rank 6
        location = a6.getLocation();
        a6.setLocation(h6.getLocation());
        h6.setLocation(location);
        location = b6.getLocation();
        b6.setLocation(g6.getLocation());
        g6.setLocation(location);
        location = c6.getLocation();
        c6.setLocation(f6.getLocation());
        f6.setLocation(location);
        location = d6.getLocation();
        d6.setLocation(e6.getLocation());
        e6.setLocation(location);

        // Flip Rank 7
        location = a7.getLocation();
        a7.setLocation(h7.getLocation());
        h7.setLocation(location);
        location = b7.getLocation();
        b7.setLocation(g7.getLocation());
        g7.setLocation(location);
        location = c7.getLocation();
        c7.setLocation(f7.getLocation());
        f7.setLocation(location);
        location = d7.getLocation();
        d7.setLocation(e7.getLocation());
        e7.setLocation(location);

        // Flip Rank 8
        location = a8.getLocation();
        a8.setLocation(h8.getLocation());
        h8.setLocation(location);
        location = b8.getLocation();
        b8.setLocation(g8.getLocation());
        g8.setLocation(location);
        location = c8.getLocation();
        c8.setLocation(f8.getLocation());
        f8.setLocation(location);
        location = d8.getLocation();
        d8.setLocation(e8.getLocation());
        e8.setLocation(location);
    }//GEN-LAST:event_flipMenuItemActionPerformed
    private void hostMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hostMenuItemActionPerformed
        hostMan = new HostManager();
        connMan = null;
        host = new HostDialog(this, true);
        host.setLocationRelativeTo(this);
        host.setVisible(true);
        
        if(hostMan.isLoad()){            
            hostMenuItem.setEnabled(false);
            connectMenuItem.setEnabled(false);
            newGameMenuItem.setEnabled(false);
            disconnectMenuItem.setEnabled(true);
            drawMenuItem.setEnabled(true);
            resignMenuItem.setEnabled(true);
            wipeBoard(); //Make a new board
            initNew();
            StreamListener stream = new StreamFetcher();            
            hostMan.addStreamListener(stream);
            updatePieces();
        }
        else{
        hostMenuItem.setEnabled(false);
        connectMenuItem.setEnabled(false);
        newGameMenuItem.setEnabled(false);
        disconnectMenuItem.setEnabled(true);
        drawMenuItem.setEnabled(true);
        resignMenuItem.setEnabled(true);
        wipeBoard(); //Make a new board
        initNew();
       	StreamListener stream = new StreamFetcher();
       	hostMan.addStreamListener(stream);
        }
    }//GEN-LAST:event_hostMenuItemActionPerformed

    public void resetHostMenuItems() {
        hostMenuItem.setEnabled(true);
        connectMenuItem.setEnabled(true);
        newGameMenuItem.setEnabled(true);
        disconnectMenuItem.setEnabled(false);
        hostMan = null;
        connMan = null;
    }

    private void requestTakeBackActionPerformed(java.awt.event.ActionEvent evt) {
        if (moveCount > 1) {
            if ((connMan == null) && (hostMan == null)) //single player, takeBack
            {
                takeBack();
            } else if ((hostMan != null) && (!currentPlayer.isMyTurn())) {
                hostMan.sendData("5");
            } else if ((connMan != null) && (!currentPlayer.isMyTurn())) {
                connMan.sendData("5");
            }
        }
    }

    private void requestDrawActionPerformed(java.awt.event.ActionEvent evt) {
        if (hostMan != null) {
            hostMan.sendData("2");
        } else if (connMan != null) {
            connMan.sendData("2");
        }
    }
// <editor-fold defaultstate="collapsed" desc="Board Click Events">

    private void a8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a8;
            clickHandler();
        }
    }//GEN-LAST:event_a8MouseClicked

    private void b8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b8;
            clickHandler();
        }
    }//GEN-LAST:event_b8MouseClicked

    private void c8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c8;
            clickHandler();
        }
    }//GEN-LAST:event_c8MouseClicked

    private void d8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d8;
            clickHandler();
        }
    }//GEN-LAST:event_d8MouseClicked

    private void e8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e8;
            clickHandler();
        }
    }//GEN-LAST:event_e8MouseClicked

    private void f8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f8;
            clickHandler();
        }
    }//GEN-LAST:event_f8MouseClicked

    private void g8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g8;
            clickHandler();
        }
    }//GEN-LAST:event_g8MouseClicked

    private void h8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h8MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h8;
            clickHandler();
        }
    }//GEN-LAST:event_h8MouseClicked

    private void a7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a7;
            clickHandler();
        }
    }//GEN-LAST:event_a7MouseClicked

    private void b7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b7;
            clickHandler();
        }
    }//GEN-LAST:event_b7MouseClicked

    private void c7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c7;
            clickHandler();
        }
    }//GEN-LAST:event_c7MouseClicked

    private void d7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d7;
            clickHandler();
        }
    }//GEN-LAST:event_d7MouseClicked

    private void e7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e7;
            clickHandler();
        }
    }//GEN-LAST:event_e7MouseClicked

    private void f7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f7;
            clickHandler();
        }
    }//GEN-LAST:event_f7MouseClicked

    private void g7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g7;
            clickHandler();
        }
    }//GEN-LAST:event_g7MouseClicked

    private void h7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h7MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h7;
            clickHandler();
        }
    }//GEN-LAST:event_h7MouseClicked

    private void a6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a6;
            clickHandler();
        }
    }//GEN-LAST:event_a6MouseClicked

    private void b6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b6;
            clickHandler();
        }
    }//GEN-LAST:event_b6MouseClicked

    private void c6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c6;
            clickHandler();
        }
    }//GEN-LAST:event_c6MouseClicked

    private void d6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d6;
            clickHandler();
        }
    }//GEN-LAST:event_d6MouseClicked

    private void e6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e6;
            clickHandler();
        }
    }//GEN-LAST:event_e6MouseClicked

    private void f6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f6;
            clickHandler();
        }
    }//GEN-LAST:event_f6MouseClicked

    private void g6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g6;
            clickHandler();
        }
    }//GEN-LAST:event_g6MouseClicked

    private void h6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h6MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h6;
            clickHandler();
        }
    }//GEN-LAST:event_h6MouseClicked

    private void a5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a5;
            clickHandler();
        }
    }//GEN-LAST:event_a5MouseClicked

    private void b5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b5;
            clickHandler();
        }
    }//GEN-LAST:event_b5MouseClicked

    private void c5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c5;
            clickHandler();
        }
    }//GEN-LAST:event_c5MouseClicked

    private void d5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d5;
            clickHandler();
        }
    }//GEN-LAST:event_d5MouseClicked

    private void e5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e5;
            clickHandler();
        }
    }//GEN-LAST:event_e5MouseClicked

    private void f5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f5;
            clickHandler();
        }
    }//GEN-LAST:event_f5MouseClicked

    private void g5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g5;
            clickHandler();
        }
    }//GEN-LAST:event_g5MouseClicked

    private void h5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h5MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h5;
            clickHandler();
        }
    }//GEN-LAST:event_h5MouseClicked

    private void a4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a4;
            clickHandler();
        }
    }//GEN-LAST:event_a4MouseClicked

    private void b4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b4;
            clickHandler();
        }
    }//GEN-LAST:event_b4MouseClicked

    private void c4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c4;
            clickHandler();
        }
    }//GEN-LAST:event_c4MouseClicked

    private void d4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d4;
            clickHandler();
        }
    }//GEN-LAST:event_d4MouseClicked

    private void e4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e4;
            clickHandler();
        }
    }//GEN-LAST:event_e4MouseClicked

    private void f4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f4;
            clickHandler();
        }
    }//GEN-LAST:event_f4MouseClicked

    private void g4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g4;
            clickHandler();
        }
    }//GEN-LAST:event_g4MouseClicked

    private void h4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h4MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h4;
            clickHandler();
        }
    }//GEN-LAST:event_h4MouseClicked

    private void a3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a3;
            clickHandler();
        }
    }//GEN-LAST:event_a3MouseClicked

    private void b3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b3;
            clickHandler();
        }
    }//GEN-LAST:event_b3MouseClicked

    private void c3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c3;
            clickHandler();
        }
    }//GEN-LAST:event_c3MouseClicked

    private void d3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d3;
            clickHandler();
        }
    }//GEN-LAST:event_d3MouseClicked

    private void e3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e3;
            clickHandler();
        }
    }//GEN-LAST:event_e3MouseClicked

    private void f3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f3;
            clickHandler();
        }
    }//GEN-LAST:event_f3MouseClicked

    private void g3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g3;
            clickHandler();
        }
    }//GEN-LAST:event_g3MouseClicked

    private void h3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h3MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h3;
            clickHandler();
        }
    }//GEN-LAST:event_h3MouseClicked

    private void a2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a2;
            clickHandler();
        }
    }//GEN-LAST:event_a2MouseClicked

    private void b2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b2;
            clickHandler();
        }
    }//GEN-LAST:event_b2MouseClicked

    private void c2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c2;
            clickHandler();
        }
    }//GEN-LAST:event_c2MouseClicked

    private void d2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d2;
            clickHandler();
        }
    }//GEN-LAST:event_d2MouseClicked

    private void e2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e2;
            clickHandler();
        }
    }//GEN-LAST:event_e2MouseClicked

    private void f2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f2;
            clickHandler();
        }
    }//GEN-LAST:event_f2MouseClicked

    private void g2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g2;
            clickHandler();
        }
    }//GEN-LAST:event_g2MouseClicked

    private void h2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h2MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h2;
            clickHandler();
        }
    }//GEN-LAST:event_h2MouseClicked

    private void h1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_h1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = h1;
            clickHandler();
        }
    }//GEN-LAST:event_h1MouseClicked

    private void g1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_g1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = g1;
            clickHandler();
        }
    }//GEN-LAST:event_g1MouseClicked

    private void f1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_f1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = f1;
            clickHandler();
        }
    }//GEN-LAST:event_f1MouseClicked

    private void e1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_e1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = e1;
            clickHandler();
        }

    }//GEN-LAST:event_e1MouseClicked

    private void d1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_d1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = d1;
            clickHandler();
        }
    }//GEN-LAST:event_d1MouseClicked

    private void c1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = c1;
            clickHandler();
        }
    }//GEN-LAST:event_c1MouseClicked

    private void b1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_b1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = b1;
            clickHandler();
        }
    }//GEN-LAST:event_b1MouseClicked

    private void a1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_a1MouseClicked
        if (currentPlayer.isMyTurn()) {
            clicked = a1;
            clickHandler();
        }
    }//GEN-LAST:event_a1MouseClicked

    private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
        if(connMan != null){
            writeFile = new WriteFile(currentPlayer.getNickName());
            writeFile.write(moveList,board.boardState(), currentPlayer.getColor());
        }
        if(hostMan != null){
        writeFile = new WriteFile(currentPlayer.getNickName()+oppPlayer);
        writeFile.write(moveList, board.boardState(), currentPlayer.getColor());
        }
    }//GEN-LAST:event_saveMenuItemActionPerformed

    private void loadMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadMenuItemActionPerformed


    }//GEN-LAST:event_loadMenuItemActionPerformed

    private void resignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resignActionPerformed
        resignDialog = new SureDialog(this, true);
        resignDialog.setLocationRelativeTo(this);
        resignDialog.setVisible(true);
    }//GEN-LAST:event_resignActionPerformed

    private void takebackMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_takebackMenuItemActionPerformed
        if (moveCount>1){ 
	    	if ((connMan == null) && (hostMan == null)) //single player, takeBack
	    		takeBack();
	    	else if ((hostMan != null)&&(!currentPlayer.isMyTurn())) hostMan.sendData("5");
	    	else if ((connMan != null)&&(!currentPlayer.isMyTurn())) connMan.sendData("5");
        }
    }//GEN-LAST:event_takebackMenuItemActionPerformed

    private void drawMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drawMenuItemActionPerformed
    	if (hostMan != null) hostMan.sendData("2");
    	if (connMan != null) connMan.sendData("2");
    }//GEN-LAST:event_drawMenuItemActionPerformed

    private void newGameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newGameMenuItemActionPerformed
        wipeBoard();
        initNew();
    }//GEN-LAST:event_newGameMenuItemActionPerformed

    private void instructionsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_instructionsMenuItemActionPerformed
        ibox = new InstructionBox();
        ibox.setLocationRelativeTo(this);
        ibox.setVisible(true);
    }//GEN-LAST:event_instructionsMenuItemActionPerformed
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="ClickHandler">
    private void clickHandler() {
        String notation = "";
        int condition=0;
    	if (((connMan == null) && (hostMan == null)) && (gameOver)){
    		return; 
    	} else {
    		if (gameOver) {
	            if (hostMan != null && hostMan.isConnected()) {
	                gameSwitch();
	                currentPlayer.setColor(Color.WHITE);  //TODO switch color and turn
	                currentPlayer.setMyTurn(true);
	            } else {
	                return;
	            }
	        } else if (!currentPlayer.isMyTurn()) {
	        	System.out.println("it's not my turn");//troubleshooter
	        }
    	} 
    	
        Color color = board.colorAt(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName()));
        if (sourceSquare == null && ((currentPlayer.isMyTurn() && color != currentPlayer.getColor()))) {
         	return;
        }

        if (sourceSquare != null && ((currentPlayer.isMyTurn() && color == currentPlayer.getColor()))) {
            sourceSquare = null;
        }

        removeBorders();
  		clicked.setBorder(BorderFactory.createLineBorder(Color.red, 2));

        if (sourceSquare == null) {
            sourceSquare = clicked;

            int[] moves = board.validMoves(getRankFromName(sourceSquare.getName()),
                    getColumnFromName(sourceSquare.getName()));

            if (moves[0] == 99) {
                removeBorders();
                sourceSquare = null;
            } else {
                for (int x = 0; moves[x] != 99; x++) {
                    String alphaBoard = "abcdefgh";
                    highlightSquare(alphaBoard.charAt(moves[x] % 10) + Integer.toString(moves[x] / 10 + 1));
                }
            }
            // </editor-fold>
        } else if (clicked == sourceSquare) {
            removeBorders();
            sourceSquare = null;
            clicked.repaint();
        } else if (sourceSquare != null) {
            // <editor-fold defaultstate="collapsed" desc="Move locking">

            int[] moves = board.validMoves(getRankFromName(sourceSquare.getName()),
                    getColumnFromName(sourceSquare.getName()));

            if (moves[0] == 99) {
                removeBorders();
                sourceSquare = clicked = null;
            } else {
                for (int x = 0; x < moves.length; x++) {
                    switch (moves[x] % 10) {
                        case 0:
                            if (("a" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 1:
                            if (("b" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 2:
                            if (("c" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 3:
                            if (("d" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 4:
                            if (("e" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 5:
                            if (("f" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 6:
                            if (("g" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 7:
                            if (("h" + new Integer(moves[x] / 10 + 1).toString()).equals(clicked.getName())) {
                                x = 50;
                            }
                            continue;
                        case 9:
                            sourceSquare = clicked = null;
                            removeBorders();
                            return;
                    }
                }//end valid/invalid
            }//end sourcesquare!=null
  
            try {
                try {
                    sourceSquare.getComponent(0);
                    // Check if en passant capture was performed
                    // <editor-fold defaultstate="collapsed" desc="en passant">
                    if (board.pieceAt(getRankFromName(sourceSquare.getName()),
                            getColumnFromName(sourceSquare.getName())).equals("P")) {
                        if (getColumnFromName(sourceSquare.getName()) != getColumnFromName(clicked.getName())) {
                            try {
                                clicked.getComponent(0);
                            } catch (Exception exc) {
                                enPassant = true;
                                capture = true;
                                if (currentPlayer.isMyTurn()) {
                                    int col = getColumnFromName(clicked.getName());
                                    switch (col) {
                                        case 0:
                                        	clearPanel("a5");
                                            break;
                                        case 1:
                                            clearPanel("b5");
                                            break;
                                        case 2:
                                        	clearPanel("c5");
                                            break;
                                        case 3:
                                            clearPanel("d5");
                                             break;
                                        case 4:
                                        	clearPanel("e5");
                                            break;
                                        case 5:
                                        	clearPanel("f5");
                                            break;
                                        case 6:
                                        	clearPanel("g5");
                                            break;
                                        default:
                                        	clearPanel("h5");
                                            break;
                                    }//end switch
                                    board.changePiece(4, col, 7, null);
                                } else {// !myturn
                                    int col = getColumnFromName(clicked.getName());
                                    switch (col) {
                                        case 0:
                                            clearPanel("a4");
                                            break;
                                        case 1:
                                        	clearPanel("b4");
                                            break;
                                        case 2:
                                        	clearPanel("c4");
                                            break;
                                        case 3:
                                        	clearPanel("d4");
                                            break;
                                        case 4:
                                        	clearPanel("e4");
                                            break;
                                        case 5:
                                        	clearPanel("f4");
                                            break;
                                        case 6:
                                        	clearPanel("g4");
                                            break;
                                        default:
                                        	clearPanel("h4");
                                            break;
                                    }//end switch
                                    board.changePiece(3, col, 7, null);
                                    //throw new ArrayIndexOutOfBoundsException();
                                }//end !myturn
                                throw new ArrayIndexOutOfBoundsException();
                            }//end clicked.getcomp
                        }
                    }//end enpassant check
                    clicked.getComponent(0);
                    clicked.remove(0);
                    capture = true;
                } catch (ArrayIndexOutOfBoundsException e) {}

                // Check for castle
                // <editor-fold defaultstate="collapsed" desc="castle">
                if (board.pieceAt(getRankFromName(sourceSquare.getName()),
                        getColumnFromName(sourceSquare.getName())).equals("K") && getColumnFromName(sourceSquare.getName()) == 4
                        && (getColumnFromName(clicked.getName()) == 2 || getColumnFromName(clicked.getName()) == 6)) {
                    castled = true;
                    if (currentPlayer.getColor()==Color.WHITE && getColumnFromName(clicked.getName()) == 2) {
                        board.movePiece(0, 0, 0, 3);
                        d1.add(a1.getComponent(0));
                        a1.repaint();
                    } else if (currentPlayer.getColor()==Color.WHITE && getColumnFromName(clicked.getName()) == 6) { 
                        board.movePiece(0, 7, 0, 5);
                        f1.add(h1.getComponent(0));
                        h1.repaint();
                    } else if (currentPlayer.getColor()==Color.BLACK && getColumnFromName(clicked.getName()) == 2) {
                        board.movePiece(7, 0, 7, 3);
                        d8.add(a8.getComponent(0));
                        a8.repaint();
                    } else {
                        board.movePiece(7, 7, 7, 5);
                        f8.add(h8.getComponent(0));
                        h8.repaint();
                    }
                }//end castling
                
                clicked.add(sourceSquare.getComponent(0));
                sourceSquare.repaint();
                
                // Check for promotion
                // <editor-fold defaultstate="collapsed" desc="promotion">
                if (currentPlayer.getColor()==Color.WHITE && board.pieceAt(getRankFromName(sourceSquare.getName()),
                        getColumnFromName(sourceSquare.getName())).equals("P")
                        && (clicked.getName().equals("a8")
                        || clicked.getName().equals("b8")
                        || clicked.getName().equals("c8")
                        || clicked.getName().equals("d8")
                        || clicked.getName().equals("e8")
                        || clicked.getName().equals("f8")
                        || clicked.getName().equals("g8")
                        || clicked.getName().equals("h8"))) {
                    promote = new PromotionDialog(this, true, true);
                    promote.setLocationRelativeTo(this);
                    promote.setVisible(true);
                    board.changePiece(getRankFromName(sourceSquare.getName()), getColumnFromName(sourceSquare.getName()), promote.promotionType, Color.white);
                    changeIcon(clicked, promote.promotionType, Color.white);
                    promote.dispose();
                    promoted = true;
                } else if (currentPlayer.getColor()==Color.BLACK && board.pieceAt(getRankFromName(sourceSquare.getName()),
                        getColumnFromName(sourceSquare.getName())).equals("P")
                        && (clicked.getName().equals("a1")
                        || clicked.getName().equals("b1")
                        || clicked.getName().equals("c1")
                        || clicked.getName().equals("d1")
                        || clicked.getName().equals("e1")
                        || clicked.getName().equals("f1")
                        || clicked.getName().equals("g1")
                        || clicked.getName().equals("h1"))) {
                    promote = new PromotionDialog(this, true, false);
                    promote.setLocationRelativeTo(this);
                    promote.setVisible(true);
                    board.changePiece(getRankFromName(sourceSquare.getName()), getColumnFromName(sourceSquare.getName()), promote.promotionType, Color.black);
                    changeIcon(clicked, promote.promotionType, Color.black);
                    promote.dispose();
                    promoted = true;
                }
                // </editor-fold>

                // Update logical locations
                board.movePiece(getRankFromName(sourceSquare.getName()),
                        getColumnFromName(sourceSquare.getName()),
                        getRankFromName(clicked.getName()),
                        getColumnFromName(clicked.getName()));
                System.out.println(board.toString());

                //check here for multiples
                if ((board.pieceAt(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName())).equals("R"))
                        || (board.pieceAt(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName())).equals("N"))
                        || (board.pieceAt(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName())).equals("B"))
                        || (board.pieceAt(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName())).equals("Q"))) {
                    multipleNotation = board.multipleCheck(getRankFromName(clicked.getName()), getColumnFromName(clicked.getName()));
                }

                notation = getMoveAlgebraic();
                if (gameOver) condition = 1;
                else if (board.draw) condition = 2;
                
                if (((connMan != null) || (hostMan != null)) && sourceSquare!=null && clicked!=null)
                {   
                	movesConfirmed = false;
                	if (connMan == null) { //host code
	                    if (currentPlayer.isMyTurn()) {  //If my turn send move
							//for (int x=0; x<20 && !movesConfirmed; x++) { //after 20 times with no confirm (10 seconds) 
								hostMan.sendData("0:" + board.boardState()  + ":" + condition + ":" + board.getLastMove() + ":" + notation); 
								wait(500); //give it a second to process
							//}
	                    	//if (!movesConfirmed){ //timed out
							//	try {
							//		hostMan.stopHost();
							//	} catch (IOException ex) {}
							//}
	                    }
                	} else if (hostMan == null) {   //Conn code
	                    if (currentPlayer.isMyTurn()) {  //If my turn send move
	                    	//for (int x=0; x<20 && !movesConfirmed; x++) { //after 20 times with no confirm (10 seconds) 
	                    		connMan.sendData("0:" + board.boardState() + ":" + condition + ":" + board.getLastMove() + ":" + notation); 
	                    		wait(500); //give it a second to process
							//}
	                    	//if (!movesConfirmed){ //timed out
							//	try {
							//		connMan.disconnect();
							//	} catch (IOException ex) {}
							//}
	                    }
	                } 
                }//end online play
                postNotation(notation);
            } catch (ArrayIndexOutOfBoundsException e) {} //no component to move
            removeBorders();
            sourceSquare = null;
         }
        clicked = null;
    }
// <editor-fold defaultstate="collapsed" desc="Helper Methods">

    private void postNotation(String notation)
    {
        if ((connMan==null) && (hostMan==null)){////////////////////single player
        	if(currentPlayer.getColor()==Color.WHITE){
        		if (!"".equals(notation)) 
        			moveList.add(moveCount + ". " + notation);
            	currentPlayer.setColor(Color.BLACK);
            	if (gameOver) {
                    if (board.draw) {
                        moveList.add("1/2 - 1/2");
                    } else {
                        moveList.add("1 - 0");
                    }
               	}
            } else { //black player
            	if (!"".equals(notation)) {
            		String s = moveList.getItem(moveCount - 1);
	            	moveList.remove(moveCount - 1);
	                moveList.add(s + " ... " + notation);
            	}
                moveCount++;
                currentPlayer.setColor(Color.WHITE);
                if (gameOver) {
                    if (board.draw) {
                        moveList.add("1/2 - 1/2");
                    } else {
                        moveList.add("0 - 1");
                    }
                }
            }
        	/////////////////////////////////////////////////////////
    	} else if ((!currentPlayer.isMyTurn() && (currentPlayer.getColor()==Color.BLACK)) ||
	    		  (currentPlayer.isMyTurn() && (currentPlayer.getColor()==Color.WHITE))){//white's turn
	    	System.out.println("notating host move");
    		if (!"".equals(notation)) //don't add the count if it was an accepted draw offer
    			moveList.add(moveCount + ". " + notation);
	        if (gameOver) {
	            if (board.draw) {
	                moveList.add("1/2 - 1/2");
	            } else {
	                moveList.add("1 - 0");
	            }
	        }
	       	removeBorders();
	    	sourceSquare = null;
	    	clicked = null;
	    	validate();
	        currentPlayer.switchTurn();
	       	if (currentPlayer.isMyTurn())
	       		takebackMenuItem.setEnabled(false);
	       	else
	       		takebackMenuItem.setEnabled(true);
    	} else if ((currentPlayer.isMyTurn() && (currentPlayer.getColor()==Color.BLACK)) ||
		    		  (!currentPlayer.isMyTurn() && (currentPlayer.getColor()==Color.WHITE))){//black's turn
    		if (!"".equals(notation)) {//don't add the count if it was an accepted draw offer
    			System.out.println("notating client move");
	        	String s = moveList.getItem(moveCount - 1);
	        	moveList.remove(moveCount - 1);
		        moveList.add(s + " ... " + notation);
		        moveCount++;
                        if(hostMan != null && connMan == null){
                            writeFile = new WriteFile(currentPlayer.getNickName()+oppPlayer);//currently nickname is set to "".
                            writeFile.write(moveList, board.boardState(), currentPlayer.getColor());
                        }
                        if(connMan != null && hostMan == null){
                            writeFile = new WriteFile(currentPlayer.getNickName());
                            writeFile.write(moveList, board.boardState(), currentPlayer.getColor());
                        }
                    }
	        if (gameOver) {
	            if (board.draw) {
	                moveList.add("1/2 - 1/2");
	            } else {
	                moveList.add("0 - 1");
	            }
	        }
	        removeBorders();
	    	sourceSquare = null;
	    	clicked = null;
	    	validate();
	       	currentPlayer.switchTurn();
	       	if (currentPlayer.isMyTurn())
	       		takebackMenuItem.setEnabled(false);
	       	else
	       		takebackMenuItem.setEnabled(true);

		}//end online play
	    capture = false;
	    multipleNotation = false;
	    if (board.draw) {
	        draw = new OKDialog(this, true, 0);
	        draw.setAlwaysOnTop(true);
	        draw.setLocationRelativeTo(this);
	        draw.setVisible(true);
        } else if (currentPlayer.isMyTurn()){//if running two boards on local host this will slow things down
            try {
            	AudioPlayer.player.start(new AudioStream(new FileInputStream("src/resources/beep_3.au")));
            } catch (FileNotFoundException e) {
            } catch (IOException e) {}
	    }
    }

    private void changeIcon(javax.swing.JPanel square, int type, Color color) {
        switch (type) {
            case 1:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wq.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bq.png")));
                }
                break;
            case 2:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wr.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/br.png")));
                }
                break;
            case 3:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wb.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bb.png")));
                }
                break;
            case 4:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wn.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bn.png")));
                }
                break;
            case 5:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png")));
                }
                break;
            case 6:
                if (color == Color.white) {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wk.png")));
                } else {
                    ((javax.swing.JLabel) square.getComponent(0)).setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bk.png")));
                }
        }

    }

    private void highlightSquare(String square) {
        Component[] comps = boardPanel.getComponents();
        for (int x = 0; x < comps.length; x++) {
            if (comps[x].getName() != null && comps[x].getName().equals(square)) {
                ((javax.swing.JPanel) comps[x]).setBorder(BorderFactory.createLineBorder(Color.green, 2));
            }
        }
    }

    private String getMoveAlgebraic() {

        if (sourceSquare == null || clicked == null) {
            return "";
        }
        String move;
        String p = board.pieceAt(getRankFromName(clicked.getName()),
                getColumnFromName(clicked.getName()));
        if (castled) {
            castled = false;
            lastMoveCastled = true;
            if (getColumnFromName(clicked.getName()) == 6) {
                return "O-O";
            } else {
                return "O-O-O";
            }
        }
        if (p.equals("P") || promoted) {
            /* This is a pawn. */
            if (capture) {
                move = "" + sourceSquare.getName().charAt(0) + 'x' + clicked.getName();
            } else {
                move = clicked.getName();
            }
            if (promoted) {
                move = move + "=" + p;
                promoted = false;
            }
            if (enPassant) {
                move = move + " e.p.";
                enPassant = false;
            }

            if (board.kingCheck(currentPlayer.getColor()==Color.WHITE)){
                board.checked = true;
                if (board.mateCheck(currentPlayer.getColor()==Color.WHITE)){
                    gameOver = true;
                    move = (move + "#");
                } else {
                    move = (move + "+");
                }
            } else if (board.mateCheck(currentPlayer.getColor()==Color.WHITE)) {
                gameOver = true;
                board.draw = true;
            }

            return move;
        }//end pawn

        move = p;
        if (multipleNotation) {
            move = (move + sourceSquare.getName());
        }

        if (capture) {
            move = (move + 'x');
        }

        if (board.kingCheck(currentPlayer.getColor()==Color.WHITE)){  //check and checkmate
        	board.checked = true;
        	if (board.mateCheck(currentPlayer.getColor()==Color.WHITE)){
        		gameOver = true;
                move = (move + clicked.getName() + "#");
            } else {
                move = (move + clicked.getName() + "+");
            }
        } else if (board.mateCheck(currentPlayer.isMyTurn())) { //stalemate
            gameOver = true;
            board.draw = true;
            move = (move + clicked.getName());
        } else {
            move = (move + clicked.getName());
        }
        return move;
    }

    private int getColumnFromName(String name) {
        switch (name.charAt(0)) {
            case 'a':
                return 0;
            case 'b':
                return 1;
            case 'c':
                return 2;
            case 'd':
                return 3;
            case 'e':
                return 4;
            case 'f':
                return 5;
            case 'g':
                return 6;
            default:
                return 7;
        }
    }

    private int getRankFromName(String name) {
        switch (name.charAt(1)) {
            case '1':
                return 0;
            case '2':
                return 1;
            case '3':
                return 2;
            case '4':
                return 3;
            case '5':
                return 4;
            case '6':
                return 5;
            case '7':
                return 6;
            default:
                return 7;
        }
    }
    // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Flip Board">
// <editor-fold defaultstate="collapsed" desc="Remove Borders">
    private void removeBorders() {
        a8.setBorder(null);
        b8.setBorder(null);
        c8.setBorder(null);
        d8.setBorder(null);
        e8.setBorder(null);
        f8.setBorder(null);
        g8.setBorder(null);
        h8.setBorder(null);
        a7.setBorder(null);
        b7.setBorder(null);
        c7.setBorder(null);
        d7.setBorder(null);
        e7.setBorder(null);
        f7.setBorder(null);
        g7.setBorder(null);
        h7.setBorder(null);
        a6.setBorder(null);
        b6.setBorder(null);
        c6.setBorder(null);
        d6.setBorder(null);
        e6.setBorder(null);
        f6.setBorder(null);
        g6.setBorder(null);
        h6.setBorder(null);
        a5.setBorder(null);
        b5.setBorder(null);
        c5.setBorder(null);
        d5.setBorder(null);
        e5.setBorder(null);
        f5.setBorder(null);
        g5.setBorder(null);
        h5.setBorder(null);
        a4.setBorder(null);
        b4.setBorder(null);
        c4.setBorder(null);
        d4.setBorder(null);
        e4.setBorder(null);
        f4.setBorder(null);
        g4.setBorder(null);
        h4.setBorder(null);
        a3.setBorder(null);
        b3.setBorder(null);
        c3.setBorder(null);
        d3.setBorder(null);
        e3.setBorder(null);
        f3.setBorder(null);
        g3.setBorder(null);
        h3.setBorder(null);
        a2.setBorder(null);
        b2.setBorder(null);
        c2.setBorder(null);
        d2.setBorder(null);
        e2.setBorder(null);
        f2.setBorder(null);
        g2.setBorder(null);
        h2.setBorder(null);
        a1.setBorder(null);
        b1.setBorder(null);
        c1.setBorder(null);
        d1.setBorder(null);
        e1.setBorder(null);
        f1.setBorder(null);
        g1.setBorder(null);
        h1.setBorder(null);
    }
// </editor-fold>

    public void takeBack() {
        int[] changes = new int[5];

        changes = board.boardChanges(lastMoveCastled);//get the changes to the logical board
        board.revertBoard(lastMoveCastled); //reset the logical board to the previous state

        updateChangedPieces(changes);
        if (lastMoveCastled) {
            lastMoveCastled = false;
        }
        //notation
        if (hostMan == null && connMan == null) {//Single player
            if (currentPlayer.getColor().equals(Color.black)) {
                moveList.remove(moveList.getItemCount() - 1);
                currentPlayer.setColor(Color.white);
            } else {
                String notation = moveList.getItem(moveList.getItemCount() - 1); // copy the line to a string

                moveList.remove(moveList.getItemCount() - 1); // Remove the item

                moveList.add(notation.substring(0, notation.indexOf(" ..."))); // add the white notation back to the list

                currentPlayer.setColor(Color.black);
                moveCount--;
            }
        } else {//networked game
            if ((currentPlayer.isMyTurn() && (currentPlayer.getColor() == Color.BLACK))
                    || (!currentPlayer.isMyTurn() && (currentPlayer.getColor() == Color.WHITE))) {//black's turn, remove white move
                moveList.remove(moveList.getItemCount() - 1);
            } else if ((!currentPlayer.isMyTurn() && (currentPlayer.getColor() == Color.BLACK))
                    || (currentPlayer.isMyTurn() && (currentPlayer.getColor() == Color.WHITE))) {//white's turn, remove black move
                String notation = moveList.getItem(moveList.getItemCount() - 1);//copy the line to a string
                moveList.remove(moveList.getItemCount() - 1);//remove the line
                moveList.add(notation.substring(0, notation.indexOf(" ...")));//add the white notation back to the list
                moveCount--;
            }
            currentPlayer.switchTurn();
            if (currentPlayer.isMyTurn()) {
                takebackMenuItem.setEnabled(false);
            } else {
                takebackMenuItem.setEnabled(true);
            }
        }//end networked player
    }

    private void updateChangedPieces(int[] changes) {
        Component[] comps = boardPanel.getComponents();
        int rank = 0;
        int column = 0;
        for (int i = 0; changes[i] >= 0; i++) {//get the logical location of the differences to be changed
            rank = (changes[i]) / 10;
            column = (changes[i]) % 10;

            for (int x = 0; x < comps.length; x++) {//index through the panels to find the ones to be updated
                if (comps[x].getName() != null && comps[x].getName().equals(getPanelFromRC(rank, column))) { //found one

                    if (board.pieceAt(rank, column).equals(" ")) {
                        ((javax.swing.JPanel) comps[x]).removeAll();
                        ((javax.swing.JPanel) comps[x]).repaint();
                    } else if (board.colorAt(rank, column) == Color.white) {
                        if (board.pieceAt(rank, column).equals("P")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whitePawnA = new javax.swing.JLabel();
                            whitePawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png")));
                            ((javax.swing.JPanel) comps[x]).add(whitePawnA);
                            whitePawnA.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("R")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whiteRook1 = new javax.swing.JLabel();
                            whiteRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wr.png")));
                            ((javax.swing.JPanel) comps[x]).add(whiteRook1);
                            whiteRook1.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("N")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whiteKnight1 = new javax.swing.JLabel();
                            whiteKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wn.png")));
                            ((javax.swing.JPanel) comps[x]).add(whiteKnight1);
                            whiteKnight1.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("B")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whiteLightBishop = new javax.swing.JLabel();
                            whiteLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wb.png")));
                            ((javax.swing.JPanel) comps[x]).add(whiteLightBishop);
                            whiteLightBishop.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("Q")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whiteQueen = new javax.swing.JLabel();
                            whiteQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wq.png")));
                            ((javax.swing.JPanel) comps[x]).add(whiteQueen);
                            whiteQueen.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("K")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            whiteKing = new javax.swing.JLabel();
                            whiteKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wk.png")));
                            ((javax.swing.JPanel) comps[x]).add(whiteKing);
                            whiteKing.setBounds(5, 5, 36, 36);
                        }
                    }//end white
                    else if (board.colorAt(rank, column) == Color.black) {
                        if (board.pieceAt(rank, column).equals("P")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackPawnA = new javax.swing.JLabel();
                            blackPawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackPawnA);
                            blackPawnA.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("R")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackRook1 = new javax.swing.JLabel();
                            blackRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/br.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackRook1);
                            blackRook1.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("N")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackKnight1 = new javax.swing.JLabel();
                            blackKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bn.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackKnight1);
                            blackKnight1.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("B")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackLightBishop = new javax.swing.JLabel();
                            blackLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bb.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackLightBishop);
                            blackLightBishop.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("Q")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackQueen = new javax.swing.JLabel();
                            blackQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bq.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackQueen);
                            blackQueen.setBounds(5, 5, 36, 36);
                        } else if (board.pieceAt(rank, column).equals("K")) {
                            ((javax.swing.JPanel) comps[x]).removeAll();
                            blackKing = new javax.swing.JLabel();
                            blackKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bk.png")));
                            ((javax.swing.JPanel) comps[x]).add(blackKing);
                            blackKing.setBounds(5, 5, 36, 36);
                        }
                    }//end black
                }//end if found piece
            }//end for loop to find pieces
        }//end list of board changes
    }
    
    public void updatePieces() {
        Component[] comps = boardPanel.getComponents();
        int rank = 0;
        int column = 0;
        while (rank <= 7 ){
        	while (column <= 7 ){
        	
        		for (int x = 0; x < comps.length; x++) {//index through the panels to find the ones to be updated
	                if (comps[x].getName() != null && comps[x].getName().equals(getPanelFromRC(rank, column))) { //found one
	
	                    if (board.pieceAt(rank, column).equals(" ")) {
	                        ((javax.swing.JPanel) comps[x]).removeAll();
	                        ((javax.swing.JPanel) comps[x]).repaint();
	                    } else if (board.colorAt(rank, column) == Color.white) {
	                        if (board.pieceAt(rank, column).equals("P")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whitePawnA = new javax.swing.JLabel();
	                            whitePawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wp.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whitePawnA);
	                            whitePawnA.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("R")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whiteRook1 = new javax.swing.JLabel();
	                            whiteRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wr.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whiteRook1);
	                            whiteRook1.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("N")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whiteKnight1 = new javax.swing.JLabel();
	                            whiteKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wn.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whiteKnight1);
	                            whiteKnight1.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("B")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whiteLightBishop = new javax.swing.JLabel();
	                            whiteLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wb.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whiteLightBishop);
	                            whiteLightBishop.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("Q")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whiteQueen = new javax.swing.JLabel();
	                            whiteQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wq.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whiteQueen);
	                            whiteQueen.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("K")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            whiteKing = new javax.swing.JLabel();
	                            whiteKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/wk.png")));
	                            ((javax.swing.JPanel) comps[x]).add(whiteKing);
	                            whiteKing.setBounds(5, 5, 36, 36);
	                        }
	                    }//end white
	                    else if (board.colorAt(rank, column) == Color.black) {
	                        if (board.pieceAt(rank, column).equals("P")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackPawnA = new javax.swing.JLabel();
	                            blackPawnA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bp.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackPawnA);
	                            blackPawnA.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("R")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackRook1 = new javax.swing.JLabel();
	                            blackRook1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/br.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackRook1);
	                            blackRook1.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("N")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackKnight1 = new javax.swing.JLabel();
	                            blackKnight1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bn.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackKnight1);
	                            blackKnight1.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("B")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackLightBishop = new javax.swing.JLabel();
	                            blackLightBishop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bb.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackLightBishop);
	                            blackLightBishop.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("Q")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackQueen = new javax.swing.JLabel();
	                            blackQueen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bq.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackQueen);
	                            blackQueen.setBounds(5, 5, 36, 36);
	                        } else if (board.pieceAt(rank, column).equals("K")) {
	                            ((javax.swing.JPanel) comps[x]).removeAll();
	                            blackKing = new javax.swing.JLabel();
	                            blackKing.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/bk.png")));
	                            ((javax.swing.JPanel) comps[x]).add(blackKing);
	                            blackKing.setBounds(5, 5, 36, 36);
	                        }
	                    }//end black
	                }//end if found piece
        		}//end for loop to find pieces
        		column++;
        	}//end column
        	 column = 0;
             rank++;
        }//end rank
       
    }

    private String getPanelFromRC(int rank, int column) {
        String panel;
        switch (column) {
            case 0:
                panel = "a";
                break;
            case 1:
                panel = "b";
                break;
            case 2:
                panel = "c";
                break;
            case 3:
                panel = "d";
                break;
            case 4:
                panel = "e";
                break;
            case 5:
                panel = "f";
                break;
            case 6:
                panel = "g";
                break;
            default:
                panel = "h";
                break;
        }
        return panel + (rank + 1);
    }

    private void wipeBoard() {
        int rank = 0;
        int column = 0;

        while (rank <= 7) {
            while (column <= 7) {
                if (ChessBoard.board[rank][column].getColor() != null) {
                    ChessBoard.board[rank][column] = new Empty();
                    clearPanel(getPanelFromRC(rank, column));
                }
                column++;
            }//end column loop
            column = 0;
            rank++;
        }//end rank loop
    }//end wipe board

    public void clearPanel(String square){
        Component[] comps = boardPanel.getComponents();
        
        for (int x = 0; x < comps.length; x++) {//index through the panels to find the ones to be cleared
        	if (comps[x].getName() != null && comps[x].getName().equals(square))
            {
        		((javax.swing.JPanel) comps[x]).remove(0);
            	((javax.swing.JPanel) comps[x]).repaint();
            }
        }//end for loop
   }

    public void processImportedData(String buffer)//post the notation and switch players here??
    {
    	System.out.println("buffer = " + buffer);//troubleshooter
    	movesConfirmed = false;
    	switch (buffer.charAt(0)) {
    	    case '0':
    	    	//int[] changes = new int[5];
    	    	board.convertHash(buffer.substring(2,66));//update the logical board
    	    	//changes = board.boardChanges(false); //get the changed squares
    	    	//updateChangedPieces(changes);//update the GUI
    	    	updatePieces();

    	    	if (buffer.charAt(67)=='1')gameOver = true;
    	    	else if (buffer.charAt(67)=='2')board.draw = true;
    	    	
    	    	board.setLastMove(Integer.parseInt(buffer.substring(69,71)));//need this for en passant to work!!
    	    	postNotation(buffer.substring(72));

//    	        if (hostMan!=null)
//    	        	hostMan.sendData("1:" + buffer.substring(72));
//    	        else if (connMan!=null)
//    	        	connMan.sendData("1:" + buffer.substring(72));
    	        break;
    	    case '1':
    			System.out.println("confirmed " + buffer.substring(72));//troubleshooter
    			movesConfirmed = true;
    	    	break;
    	    case '2':
    	    	System.out.println("draw request");
    	    	requestDraw = new RequestDrawDialog(this, true, connMan, hostMan);
    	        requestDraw.setLocationRelativeTo(this);
    	        requestDraw.setVisible(true);
    	    	break;
    	    case '3':
    	    	System.out.println("draw confirmed");
    	    	drawAccepted();
    	    	break;
    	    case '4':
    	    	System.out.println("draw denied");
    	    	deniedDialog = new OKDialog(this, true, 1);
    	    	deniedDialog.setLocationRelativeTo(this);
    	    	deniedDialog.setVisible(true);
    	    	break;
    	    case '5':
    	    	System.out.println("takeback request");
    	    	requestTB = new RequestTakeBackDialog(this, true, connMan, hostMan);
    	    	requestTB.setLocationRelativeTo(this);
    	    	requestTB.setVisible(true);
    	    	break;
    	    case '6':
    	    	System.out.println("takeback confirmed");
    	    	takeBack();
    	        break;
    	    case '7':
    	    	System.out.println("takeback denied");
    	    	deniedDialog = new OKDialog(this, true, 2);
    	    	deniedDialog.setLocationRelativeTo(this);
    	    	deniedDialog.setVisible(true);
    	        break;
    	    case '8':
    	    	System.out.println("opponent resigned");
    	    	quitDialog = new OKDialog(this, true, 5);
    	    	quitDialog.setLocationRelativeTo(this);
    	    	quitDialog.setVisible(true);
    	    	disconnectMenuItemActionPerformed(null);
    	    	gameOver = true;
    	    	postNotation("");
    	        break;
    	    case '9':
    	    	System.out.println("setting color");
    	    	if (buffer.charAt(2)=='0'){
	    	    	currentPlayer.setColor(Color.WHITE);
	    	        currentPlayer.setMyTurn(true);
	    	        setColor=true;
	    	        System.out.println("I'm white");
    	    	}else if (buffer.charAt(2)=='1'){
	    	    	currentPlayer.setColor(Color.BLACK);
	    	        currentPlayer.setMyTurn(false);
	    	        flipMenuItemActionPerformed(null);
	    	        setColor=true;
	    	        System.out.println("I'm black");
    	    	}
    	    	if (hostMan!=null && setColor){
    	    		hostMan.sendData("A:");
    	    	}
    	        break;
    	    case 'A':
    	    	System.out.println("host confirmed color");
    	    	colorConfirmed=true;
    	    	break; 	        
    	    default:
    	    	System.out.println("que?");
    	    	System.out.println(buffer);
    	    	break;
    	}//end case switch
    }//end processimported data  


    public ConnectionManager getConnMan() {
        return connMan;
    }

    public HostManager getHostMan() {
        return hostMan;
    }

    public void drawAccepted() {
        board.draw = true;
        gameOver = true;
        postNotation("");
    }

    public void resign() {
        // TODO: save game once resigns
        gameOver = true;
        if (hostMan != null) {
            hostMan.sendData("8:");
        } else if (connMan != null) {
            connMan.sendData("8:");
        }
        disconnectMenuItemActionPerformed(null);
    }

    public void hostFail() {
        hostFail = new OKDialog(this, true, 4);
        hostFail.setLocationRelativeTo(this);
        hostFail.setVisible(true);
    }

    public Player getCurrentPlayer() {
        return currentPlayer;
    }

    public void gameSwitch() {
        gameOver ^= true;
    }
  
    public static void wait(int n) {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do {
            t1 = System.currentTimeMillis();
        } while ((t1 - t0) < n);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Vars">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel a1;
    private javax.swing.JPanel a2;
    private javax.swing.JPanel a3;
    private javax.swing.JPanel a4;
    private javax.swing.JPanel a5;
    private javax.swing.JPanel a6;
    private javax.swing.JPanel a7;
    private javax.swing.JPanel a8;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JPanel b1;
    private javax.swing.JPanel b2;
    private javax.swing.JPanel b3;
    private javax.swing.JPanel b4;
    private javax.swing.JPanel b5;
    private javax.swing.JPanel b6;
    private javax.swing.JPanel b7;
    private javax.swing.JPanel b8;
    private javax.swing.JLabel blackDarkBishop;
    private javax.swing.JLabel blackKing;
    private javax.swing.JLabel blackKnight1;
    private javax.swing.JLabel blackKnight2;
    private javax.swing.JLabel blackLightBishop;
    private javax.swing.JLabel blackPawnA;
    private javax.swing.JLabel blackPawnB;
    private javax.swing.JLabel blackPawnC;
    private javax.swing.JLabel blackPawnD;
    private javax.swing.JLabel blackPawnE;
    private javax.swing.JLabel blackPawnF;
    private javax.swing.JLabel blackPawnG;
    private javax.swing.JLabel blackPawnH;
    private javax.swing.JLabel blackQueen;
    private javax.swing.JLabel blackRook1;
    private javax.swing.JLabel blackRook2;
    private javax.swing.JLabel boardLabel;
    private javax.swing.JPanel boardPanel;
    private javax.swing.JPanel c1;
    private javax.swing.JPanel c2;
    private javax.swing.JPanel c3;
    private javax.swing.JPanel c4;
    private javax.swing.JPanel c5;
    private javax.swing.JPanel c6;
    private javax.swing.JPanel c7;
    private javax.swing.JPanel c8;
    private javax.swing.JMenuItem connectMenuItem;
    private javax.swing.JPanel d1;
    private javax.swing.JPanel d2;
    private javax.swing.JPanel d3;
    private javax.swing.JPanel d4;
    private javax.swing.JPanel d5;
    private javax.swing.JPanel d6;
    private javax.swing.JPanel d7;
    private javax.swing.JPanel d8;
    private javax.swing.JMenuItem disconnectMenuItem;
    private javax.swing.JMenuItem drawMenuItem;
    private javax.swing.JPanel e1;
    private javax.swing.JPanel e2;
    private javax.swing.JPanel e3;
    private javax.swing.JPanel e4;
    private javax.swing.JPanel e5;
    private javax.swing.JPanel e6;
    private javax.swing.JPanel e7;
    private javax.swing.JPanel e8;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JPanel f1;
    private javax.swing.JPanel f2;
    private javax.swing.JPanel f3;
    private javax.swing.JPanel f4;
    private javax.swing.JPanel f5;
    private javax.swing.JPanel f6;
    private javax.swing.JPanel f7;
    private javax.swing.JPanel f8;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JPopupMenu.Separator fileMenuSeparator1;
    private javax.swing.JMenuItem flipMenuItem;
    private javax.swing.JPanel g1;
    private javax.swing.JPanel g2;
    private javax.swing.JPanel g3;
    private javax.swing.JPanel g4;
    private javax.swing.JPanel g5;
    private javax.swing.JPanel g6;
    private javax.swing.JPanel g7;
    private javax.swing.JPanel g8;
    private javax.swing.JMenu gameMenu;
    private javax.swing.JPopupMenu.Separator gameMenuSeparator1;
    private javax.swing.JPopupMenu.Separator gameMenuSeparator2;
    private javax.swing.JPanel h1;
    private javax.swing.JPanel h2;
    private javax.swing.JPanel h3;
    private javax.swing.JPanel h4;
    private javax.swing.JPanel h5;
    private javax.swing.JPanel h6;
    private javax.swing.JPanel h7;
    private javax.swing.JPanel h8;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem hostMenuItem;
    private javax.swing.JMenuItem instructionsMenuItem;
    private javax.swing.JMenuItem loadGameMenuItem;
    private javax.swing.JMenuBar menuBar;
    private java.awt.List moveList;
    private javax.swing.JMenuItem newGameMenuItem;
    private javax.swing.JMenuItem resignMenuItem;
    private javax.swing.JMenuItem saveGameMenuItem;
    private javax.swing.JMenuItem takebackMenuItem;
    private javax.swing.JLabel whiteDarkBishop;
    private javax.swing.JLabel whiteKing;
    private javax.swing.JLabel whiteKnight1;
    private javax.swing.JLabel whiteKnight2;
    private javax.swing.JLabel whiteLightBishop;
    private javax.swing.JLabel whitePawnA;
    private javax.swing.JLabel whitePawnB;
    private javax.swing.JLabel whitePawnC;
    private javax.swing.JLabel whitePawnD;
    private javax.swing.JLabel whitePawnE;
    private javax.swing.JLabel whitePawnF;
    private javax.swing.JLabel whitePawnG;
    private javax.swing.JLabel whitePawnH;
    private javax.swing.JLabel whiteQueen;
    private javax.swing.JLabel whiteRook1;
    private javax.swing.JLabel whiteRook2;
    // End of variables declaration//GEN-END:variables
// </editor-fold>
    private OKDialog hostFail;
    private OKDialog quitDialog;
    private SureDialog resignDialog;
    private RequestTakeBackDialog requestTB;
    private RequestDrawDialog requestDraw;
    private PromotionDialog promote;
    private InstructionBox ibox;
    private AboutBox abox;
    private OKDialog deniedDialog;
    private OKDialog draw;
    private ConnectDialog conn;
    private ConnectionManager connMan;
    private HostDialog host;
    private HostManager hostMan;
    public static ChessBoard board;
    private javax.swing.JPanel clicked;
    private javax.swing.JPanel sourceSquare;
    private boolean whiteAligned;
    private boolean capture;
    private boolean promoted;
    private boolean castled;
    private boolean enPassant;
    private boolean gameOver = false;
    private int moveCount;
    public static Player currentPlayer;
    private boolean multipleNotation;
    private boolean lastMoveCastled = false;//used for takeback
    private boolean movesConfirmed = false;
    private WriteFile writeFile;
    private boolean newGame = true;
    private boolean setColor = false;
    private boolean colorConfirmed = false;
    public static String oppPlayer;

    public class StreamFetcher implements StreamListener {

        public void streamReceived(StreamEvent event) {
            String data = "";

            try {
                if (hostMan == null) {
                    data = connMan.receiveData();
                } else if (connMan == null) {
                    data = hostMan.receiveData();
                }
                if (data != null) {
                    processImportedData(data);
                }
            } catch (IOException e) {
                Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, e);
            } catch (BufferNotReadyException e) {
                Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, e);
            }

        }
    }

}
